﻿using PLCS.Domain.Shared.PLCConfigs;

namespace PLCS.Domain.PLCConfigs;

/// <summary>
/// PLC数据读取实体类
/// </summary>
public class PLCReadData : BaseEntity
{
    /// <summary>
    /// 字段编号
    /// </summary>
    public string PLCReadDataNo { get; init; }

    /// <summary>
    /// 外键
    /// </summary>
    public Guid PLCConfigId { get; set; }

    public PLCConfig PLCConfig { get; set; }

    /// <summary>
    /// 开始位
    /// </summary>
    public int StartBit { get; set; }

    /// <summary>
    /// 结束位
    /// </summary>
    public int EndBit { get; set; }

    /// <summary>
    /// 最新数据
    /// </summary>
    public string? CurrentValue { get; private set; }

    /// <summary>
    /// 历史数据
    /// </summary>
    public string? HistoricalValue { get; set; }

    /// <summary>
    /// 读取数据类型
    /// </summary>
    public DataType DataType { get; set; }

    /// <summary>
    /// 长度
    /// </summary>
    public int Length { get; set; }

    /// <summary>
    /// 备注
    /// </summary>
    public string Remarks { get; set; }

    /// <summary>
    /// 是否监听
    /// </summary>
    ///
    public YesNo IsDependency { get; set; }

    /// <summary>
    /// 关联的字段名
    /// </summary>
    public string? CorrespondingField { get; set; }

    /// <summary>
    /// 关联的字段名
    /// </summary>
    public string? CorrespondingTable { get; set; }

    /// <summary>
    /// 关联的类型
    /// </summary>
    public string? CorrespondingType { get; set; }

    /// <summary>
    /// 关联的Where字段名
    /// </summary>
    public string? WhereField { get; set; }

    /// <summary>
    /// 关联的where字段值
    /// </summary>
    public string? WhereValue { get; set; }

    public PLCReadData()
    {
    }

    public PLCReadData(Guid pLCConfigId, string pLCReadDataNo, int startBit, int endBit, DataType dataType, string remarks, YesNo isDependency, string? correspondingField = null, string? correspondingTable = null, string? correspondingType = null, string? whereField = null, string? whereValue = null)
    {
        PLCConfigId = pLCConfigId;
        PLCReadDataNo = pLCReadDataNo;
        StartBit = startBit;
        EndBit = endBit;
        DataType = dataType;
        Remarks = remarks;
        IsDependency = isDependency;
        CorrespondingField = correspondingField;
        CorrespondingTable = correspondingTable;
        CorrespondingType = correspondingType;
        WhereField = whereField;
        WhereValue = whereValue;
        CreationTime = DateTime.Now;
    }

    public PLCReadData ChangeValue(string value)
    {
        if (value == CurrentValue)
            return this;
        HistoricalValue = CurrentValue;
        CurrentValue = value;
        LastModificationTime = DateTime.Now;
        if (IsDependency == YesNo.是)
            AddLocalEvent(new PLCReadDataValueChangeEvent()
            {
                CurrentValue = value,
                CorrespondingField = CorrespondingField,
                CorrespondingTable = CorrespondingTable,
                CorrespondingType = CorrespondingType,
                WhereField = WhereField,
                WhereValue = WhereValue
            });
        return this;
    }
}